<template>
  <div id="app">
    <el-tree
      :data= "data"
      :props="defaultProps"
      @node-click="handleNodeClick"
      show-checkbox
      node-key="catId"
      :expand-on-click-node="false"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <!-- 只有一级和二级可追加 -->
          <el-button v-if="node.level <= 2" type="text" size="mini" @click="() => append(data)">追加</el-button>
          <!-- 只有当前节点没有子节点的时候才可以删除 -->
          <el-button v-if="node.childNodes.length === 0" type="text" size="mini" @click="() => remove(node, data)">删除</el-button>
        </span>
      </span>
    </el-tree>
    <el-dialog title="新增分类" :visible.sync="dialogFormVisible">
      <el-form :model="category">
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="addCategory">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      data: [],
      defaultProps: {
        children: 'childCategory',
        label: 'name'
      },
      dialogFormVisible: false,
      category: {
        name: '',
        parentCid: '',
        showStatus: 1,
        sort: 0
      },
      formLabelWidth: '250px'
    }
  },
  methods: {
    addCategory () {
      this.$http({
        url: this.$http.adornUrl('/product/pmscategory/save'),
        method: 'post',
        data: this.$http.adornData(this.category, false)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          console.log(data)
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.dialogFormVisible = false
              this.category.name = ''
              this.getCategoryListTree()
              this.expandedKeys = [this.category.parentCid]
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    append (data) {
      this.dialogFormVisible = true
      this.category.parentCid = data.catId
      this.category.catLevel = data.catLevel * 1 + 1
    },
    remove (node, data) {
      var ids = [data.catId]
      this.$confirm(`此操作将永久删除该数据&#8203;``oaicite:{"number":1,"invalid_reason":"Malformed citation 【${data.name}】"}``&#8203;, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/product/category/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            console.log(data)
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getCategoryListTree()
                this.expandedKeys = [node.parent.data.parentCid]
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleNodeClick (data) {
      console.log(data)
    },
    getCategoryListTree () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/product/pmscategory/list'),
        method: 'get'
      }).then(({ data }) => {
        console.log(data)
        this.data = data.data
        this.dataListLoading = false
      })
    }
  },
  created () {
    this.getCategoryListTree()
  }
}
</script>

<style>
</style>
